﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TCP_Client
{
   

        class Data
        {
            //字节数组转换十六进制字符串
            public string byteToHexStr(byte[] bytes, int buffer_length)
            {
                string returnStr = "";

                if (bytes != null)
                {
                    for (int i = 0; i < buffer_length; i++)
                    {
                        returnStr += bytes[i].ToString("X2").Insert(2, " ");//两个字符之间加入空格

                    }

                }
                return returnStr;

            }

            //16进制字符串转换字节数组
            public byte[] HexStrTobyte(string strstr)
            {
                string[] strArr = strstr.Trim().Split(' ');//移除头尾空格+按照空格
                byte[] b = new byte[strArr.Length];

                for (int i = 0; i < strArr.Length; i++)   //逐个字符变为16进制字节数据
                {
                    b[i] = Convert.ToByte(strArr[i], 16);
                }

                return b;
            }
        }
}
